<template>
  <div class="login-container">
    <div class="floating-shapes">
      <div class="shape shape-1"></div>
      <div class="shape shape-2"></div>
      <div class="shape shape-3"></div>
    </div>

    <div class="login-box animated-box">
      <div class="login-left">
        <div class="welcome-text">
          <div class="animated-logo">
            <div class="logo-circle"></div>
          </div>
          <h2>欢迎回来</h2>
          <p>美多优选后台管理系统</p>
        </div>
      </div>

      <div class="login-right">
        <div class="login-form">
          <div class="logo-container">
<!--            <img src="@/assets/logo.png" alt="Logo" class="logo">-->
            <h1>管理员登录</h1>
          </div>

          <el-form
              :model="form"
              :rules="rules"
              ref="userForm"
              label-width="0"
              class="form-content"
          >
            <el-form-item prop="username">
              <div class="input-with-icon">
                <i class="el-icon-user input-icon"></i>
                <el-input
                    v-model="form.username"
                    placeholder="请输入用户名"
                    class="rounded-input"
                >
                </el-input>
              </div>
            </el-form-item>

            <el-form-item prop="password">
              <div class="input-with-icon">
                <i class="el-icon-lock input-icon"></i>
                <el-input
                    v-model="form.password"
                    type="password"
                    placeholder="请输入密码"
                    show-password
                    class="rounded-input"
                >
                </el-input>
              </div>
            </el-form-item>

            <el-form-item>
              <el-button
                  type="primary"
                  class="gradient-btn"
                  @click="login"
                  :loading="loading"
              >
                <span v-if="!loading">登 录</span>
                <i v-else class="el-icon-loading"></i>
              </el-button>
            </el-form-item>

            <div class="login-footer">
              <span>© 2025 美多优选后台管理系统</span>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useRouter } from "vue-router";
import axios from "axios";

const router = useRouter();
const form = ref({
  username: "",
  password: ""
});
const userForm = ref();
const loading = ref(false); // 新增加载状态

const rules = {
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 1, max: 20, message: "用户名在 1~20 个字符之间", trigger: "blur" },
    {
      validator: (rule, value, callback) => {
        axios
            .get("admin/validatorUsername", { params: { username: value } })
            .then((res) => {
              if (res.data) {
                callback();
              } else {
                callback(new Error("用户名不存在"));
              }
            })
            .catch(err => {
              callback(new Error("验证失败，请重试"));
            });
      },
      trigger: "blur"
    }
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, max: 20, message: "密码需 6~20 个字符", trigger: "blur" },
    {
      validator: (rule, value, callback) => {
        // 确保用户名已经验证通过
        if (!form.value.username) {
          callback(new Error("请先输入用户名"));
          return;
        }

        axios
            .post("admin/validatorPassword", form.value)
            .then((res) => {
              if (res.data) {
                callback();
              } else {
                callback(new Error("密码不正确")); // 修改错误提示
              }
            })
            .catch(err => {
              callback(new Error("验证失败，请重试"));
            });
      },
      trigger: "blur"
    }
  ]
};
const login = () => {
  loading.value = true; // 开始加载
  userForm.value.validate(valid => {
    if (valid) {
      axios.post("admin/login", form.value).then(res => {
        setTimeout(() => { // 增加延时效果，更好的UI体验
          alert(res.data.msg);
          if (res.data.code == 200) {
            sessionStorage.setItem("adminToken", res.data.data);
            sessionStorage.setItem("adminUsername", form.value.username);
            router.push('/admin/home');
          }
          loading.value = false; // 结束加载
        }, 600);
      }).catch(() => {
        loading.value = false;
      });
    } else {
      loading.value = false;
    }
  });
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
  overflow: hidden;
  position: relative;
}

.floating-shapes {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  pointer-events: none;
}

.shape {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(5px);
}

.shape-1 {
  width: 300px;
  height: 300px;
  top: 10%;
  right: 20%;
  animation: float 15s infinite ease-in-out;
}

.shape-2 {
  width: 180px;
  height: 180px;
  bottom: 15%;
  left: 15%;
  animation: float 18s infinite ease-in-out reverse;
}

.shape-3 {
  width: 120px;
  height: 120px;
  top: 40%;
  left: 20%;
  animation: float 12s infinite ease-in-out;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0) translateX(0) rotate(0deg);
  }
  25% {
    transform: translateY(-30px) translateX(20px) rotate(15deg);
  }
  50% {
    transform: translateY(20px) translateX(-15px) rotate(-10deg);
  }
  75% {
    transform: translateY(-10px) translateX(15px) rotate(5deg);
  }
}

.login-box {
  width: 900px;
  height: 520px;
  background: white;
  border-radius: 24px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.15);
  display: flex;
  overflow: hidden;
  position: relative;
  z-index: 10;
}

.animated-box {
  animation: appear 0.8s cubic-bezier(0.23, 1, 0.32, 1);
}

@keyframes appear {
  0% {
    opacity: 0;
    transform: translateY(40px) scale(0.95);
  }
  100% {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.login-left {
  flex: 1;
  background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  position: relative;
  overflow: hidden;
}

.login-left::before {
  content: "";
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.15) 0%, transparent 60%);
  opacity: 0.8;
  animation: rotate 30s linear infinite;
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.welcome-text {
  color: white;
  text-align: center;
  z-index: 2;
}

.welcome-text h2 {
  font-size: 2.8em;
  margin-bottom: 20px;
  font-weight: 700;
  letter-spacing: 1px;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.15);
}

.welcome-text p {
  font-size: 1.3em;
  letter-spacing: 1px;
  opacity: 0.92;
  font-weight: 300;
}

.animated-logo {
  margin-bottom: 40px;
  position: relative;
}

.logo-circle {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.12);
  margin: 0 auto;
  position: relative;
  animation: pulse 4s infinite ease-in-out;
}

.logo-circle::before {
  content: "";
  position: absolute;
  width: 60px;
  height: 60px;
  border: 3px solid rgba(255, 255, 255, 0.3);
  border-top-color: transparent;
  border-radius: 50%;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  animation: spin 4s infinite linear;
}

@keyframes spin {
  to {
    transform: translate(-50%, -50%) rotate(360deg);
  }
}

@keyframes pulse {
  0%, 100% {
    transform: scale(1);
    opacity: 0.8;
  }
  50% {
    transform: scale(1.05);
    opacity: 1;
  }
}

.login-right {
  flex: 1;
  padding: 60px 40px;
  display: flex;
  align-items: center;
  background: #ffffff;
}

.login-form {
  width: 100%;
  max-width: 360px;
  margin: 0 auto;
}

.logo-container {
  text-align: center;
  margin-bottom: 45px;
}

.logo {
  width: 70px;
  margin-bottom: 25px;
}

.logo-container h1 {
  font-size: 1.8em;
  color: #2c3e50;
  margin: 0;
  font-weight: 600;
  letter-spacing: 1px;
}

.form-content {
  position: relative;
  z-index: 2;
}

.input-with-icon {
  position: relative;
}

.input-icon {
  position: absolute;
  left: 16px;
  top: 50%;
  transform: translateY(-50%);
  color: #7f8c8d;
  font-size: 18px;
  z-index: 10;
}

.rounded-input :deep(.el-input__inner) {
  padding-left: 45px !important;
  height: 50px;
  border-radius: 12px;
  border: 1px solid #e6e9ee;
  background: #f7f9fc;
  transition: all 0.3s ease;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.03);
}

.rounded-input :deep(.el-input__inner:focus) {
  border-color: #3498db;
  box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2);
}

.gradient-btn {
  width: 100%;
  height: 50px;
  font-size: 16px;
  border-radius: 12px;
  background: linear-gradient(to right, #3498db, #2575fc);
  border: none;
  transition: all 0.4s ease;
  font-weight: 500;
  letter-spacing: 1px;
  box-shadow: 0 5px 15px rgba(37, 117, 252, 0.3);
  margin-top: 10px;
}

.gradient-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(37, 117, 252, 0.4);
}

.login-footer {
  text-align: center;
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid #eee;
  color: #95a5a6;
  font-size: 13px;
  letter-spacing: 0.5px;
}
</style>